<?php
declare (strict_types = 1);

namespace app\controller;

use app\common\model\Ad;
use app\common\model\Content;
use think\facade\Cache;
use think\facade\View;
use think\Request;

class Document extends Common
{

    public function __construct()
    {
        parent::__construct();
        $ad = Ad::where('board_id',7)->select();
        View::assign("ad",$ad);
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $cid = input("get.cid");
        if($cid){
            $where["cate_id"] = $cid;
        }
        $where["status"] =1;
        $where["model_id"] =7;
        if(!isset($where)){
            $where = 1;
        }

        $list = \app\common\model\Content::with("user")
            ->where($where)
            ->order("top DESC,recommend DESC,id DESC")
            ->paginate([
                'query'=> $this->params,
            ]);

        $temp = $list->items();
        foreach ($temp as $k => $v){
            //更新文章的展现量
            update_hits($v["id"]);
        }
        View::assign("list",$list);
        return  view();
    }



    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $user = $this->user;

        $id = input("get.id"); //文档的id
        if($id){
            $res = Content::find($id);
        }else{
            if (\request()->isAjax()){
                $data['model_id'] = 8;//文档标题
                $data['status'] =1; //草稿
                $data['pid'] = \request()->param("pid");
                $data['title'] = \request()->param("title");
            }else{
                $data['model_id'] = 7;//文档标题
                $data['status'] =2; //草稿
                $data['title'] = "待提交文档标题";
            }

            $data['author'] = $user["nickname"];
            $data['uid'] =$user["id"];
            $data['from'] = '文档创建';
            $res = Content::create($data);
            $id = $res->id;
            if (\request()->isAjax()){
                $data["id"] = $id;
                return json(["code"=>200,"msg"=>"成功","data"=>$data]);
            }else{
                return redirect((string)url("Document/create",["id"=>$id]));
            }

        }
        $res = Content::find($id);
        View::assign("res",$res);
        View::assign("id",$id);

        //文档的左侧菜单
        $menu = Content::with(["child"])
            ->where("pid",$id)
            ->field("id,title,pid")
            ->select()->toArray();
        foreach ($menu as $k => $v){
            $menu[$k]["open"] = 0;
        }
        //p($menu);die;
        View::assign("menu",$menu);


        return  view();
    }

    public function content_edit()
    {
        $id = \request()->param("id");
        $res = Content::find($id);
        View::assign("res",$res);
        return  view();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $res = Content::find($request->param("id"));
        if($request->param("title")){
            $res->title = $request->param("title");
        }
        if($request->param("info")){
            $res->info = $request->param("info");
        }
        $res->status = 1;//发布

        $data = $res->save();
        if ($data){
            return json(["code"=>200,"msg"=>"success","data"=>$data]);
        }else{
            return json(["code"=>400,"msg"=>"无需更新","data"=>$data]);
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $user = $this->user;

        $id = input("get.id"); //文档的id 标题
        $res = Content::find($id);
        View::assign("res",$res);
        View::assign("id",$id);

        //文档详情
        $pid = \request()->param("contentid");
        $content = Content::find($pid);
        //替换文章中的图片为配置的cdn前缀
        if(isset($content["info"])){
            $content["info"] = str_replace(\request()->domain(),config('web.cdn'),$content["info"]);
        }
        View::assign("content",$content);

        //文档的左侧菜单
        $menu = Content::with(["child"])
            ->where("pid",$id)
            ->field("id,title,pid")
            ->select()->toArray();
        foreach ($menu as $k => $v){
            $menu[$k]["open"] = 0;
        }
        //p($menu);die;
        View::assign("menu",$menu);


        return  view();
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    public function cache_clear()
    {
        Cache::clear();
    }


}
